----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

EA_TrialAlertWindow = {}

----------------------------------------------------------------
-- Saved Variables
----------------------------------------------------------------
EA_TrialAlertWindow.IsFirstRun = true

----------------------------------------------------------------
-- Local Variables
----------------------------------------------------------------

----------------------------------------------------------------
-- EA_TrialAlertWindow Functions
----------------------------------------------------------------

function EA_TrialAlertWindow.Initialize()       

    RegisterEventHandler(SystemData.Events.TRIAL_ALERT_POPUP,          "EA_TrialAlertWindow.Show")
    RegisterEventHandler(SystemData.Events.LOADING_END,                "EA_TrialAlertWindow.EnteredGame")

    ButtonSetText("EA_TrialAlertWindowCloseButton", GetString(StringTables.Default.LABEL_CLOSE) )
	ButtonSetText("EA_TrialAlertWindowUpgradeButton", GetString(StringTables.Default.LABEL_UPGRADE))
end

function EA_TrialAlertWindow.Show(alertType)

    if( alertType == nil or
        alertType == SystemData.TrialAlert.ALERT_MAIL or
        alertType == SystemData.TrialAlert.ALERT_AUCTION or
        alertType == SystemData.TrialAlert.ALERT_ZONE ) 
    then
 
        LabelSetText( "EA_TrialAlertWindowTitleBarText", GetString(StringTables.Default.TEXT_TRIAL_ALERT_NOT_ALLOWED))
        LabelSetText( "EA_TrialAlertWindowLabel", GetString(StringTables.Default.LABEL_TRIAL_ALERT_NOT_ALLOWED) )

    elseif( alertType == SystemData.TrialAlert.ALERT_FIRST_ENTRY ) 
    then
    
        LabelSetText( "EA_TrialAlertWindowTitleBarText", GetString(StringTables.Default.TEXT_TRIAL_ALERT_FIRST_ENTRY))
        LabelSetText( "EA_TrialAlertWindowLabel", GetString(StringTables.Default.LABEL_TRIAL_ALERT_FIRST_ENTRY) )

    elseif( alertType == SystemData.TrialAlert.ALERT_SERVER ) 
    then

        LabelSetText( "EA_TrialAlertWindowTitleBarText", GetString(StringTables.Default.TEXT_TRIAL_ALERT_NOT_ALLOWED))
        LabelSetText( "EA_TrialAlertWindowLabel", GetString(StringTables.Default.LABEL_TRIAL_ALERT_NOT_ALLOWED_SERVER) )
        
    elseif( alertType == SystemData.TrialAlert.ALERT_LEVEL3 or
            alertType == SystemData.TrialAlert.ALERT_LEVEL6 or
            alertType == SystemData.TrialAlert.ALERT_LEVEL9 ) 
    then
    
        LabelSetText( "EA_TrialAlertWindowTitleBarText", GetString(StringTables.Default.TEXT_TRIAL_ALERT_LEVEL))
        LabelSetText( "EA_TrialAlertWindowLabel", GetString(StringTables.Default.LABEL_TRIAL_ALERT_LEVEL) )

    elseif( alertType == SystemData.TrialAlert.ALERT_LEVEL10 )
    then
    
        LabelSetText( "EA_TrialAlertWindowTitleBarText", GetString(StringTables.Default.TEXT_TRIAL_ALERT_LEVEL10))
        LabelSetText( "EA_TrialAlertWindowLabel", GetString(StringTables.Default.LABEL_TRIAL_ALERT_LEVEL10) )
    
    else
    
        LabelSetText( "EA_TrialAlertWindowTitleBarText", GetString(StringTables.Default.TEXT_TRIAL_ALERT_NOT_ALLOWED))
        LabelSetText( "EA_TrialAlertWindowLabel", GetString(StringTables.Default.LABEL_TRIAL_ALERT_NOT_ALLOWED) )

    end

    WindowSetShowing("EA_TrialAlertWindow", true)
end

function EA_TrialAlertWindow.Close()
    WindowSetShowing("EA_TrialAlertWindow", false)
end

function EA_TrialAlertWindow.OnUpgrade()

    -- display a confirmation dialog 
    DialogManager.MakeTwoButtonDialog(GetString(StringTables.Default.LABEL_EXIT_CONFIRMATION),
        GetString(StringTables.Default.LABEL_YES), EA_TrialAlertWindow.OpenUpgradePage, 
        GetString(StringTables.Default.LABEL_NO), EA_TrialAlertWindow.Close,
        nil, nil, false, nil, nil)

end

function EA_TrialAlertWindow.OnUpgradeWithOutClose()

    -- display a confirmation dialog 
    DialogManager.MakeTwoButtonDialog(GetString(StringTables.Default.LABEL_EXIT_CONFIRMATION),
        GetString(StringTables.Default.LABEL_YES), EA_TrialAlertWindow.OpenUpgradePage, 
        GetString(StringTables.Default.LABEL_NO), nil,
        nil, nil, false, nil, nil)

end

function EA_TrialAlertWindow.OpenUpgradePage()
	
    OpenURL( GameData.URLs.URL_ACCOUNT_UPGRADE )

    -- this one is for pregame (will not get handled if you are in game)
    BroadcastEvent( SystemData.Events.QUIT )

    -- this one is for in game (will not get handled if you are in pregame)
    BroadcastEvent( SystemData.Events.EXIT_GAME )

end

function EA_TrialAlertWindow.EnteredGame()

    local isTrial, _ = GetAccountData()

    if( isTrial and EA_TrialAlertWindow.IsFirstRun)
    then
        EA_TrialAlertWindow.Show(SystemData.TrialAlert.ALERT_FIRST_ENTRY)
        EA_TrialAlertWindow.IsFirstRun = false
    end
        
end
